//
//  HCAddNewAddressController.m
//  HappyChildren
//
//  Created by jiayibai-02por on 2017/6/10.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCAddNewAddressController.h"
#import "AddressPickerView.h"


@interface HCAddNewAddressController ()<AddressPickerViewDelegate>
{
    BOOL isClicked_;
}
@property (nonatomic ,strong) AddressPickerView * pickerView;
@property (weak, nonatomic) IBOutlet UITextField *usernameText;
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UIView *chooseView;
@property (weak, nonatomic) IBOutlet UITextField *addressText;
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITextView *addressTextView;
@property (weak, nonatomic) IBOutlet UILabel *proviceLabel;
@property (nonatomic, copy) NSString *addressString;
@property (nonatomic, copy) NSString *finalyAddrss;

@end

@implementation HCAddNewAddressController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    YBKShareAppDelegate.window.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
    self.centerView.hidden = YES;
    self.chooseView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewClickAction:)];
    [self.chooseView addGestureRecognizer:tap];
    [self.saveButton addTarget:self action:@selector(saveButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.bottomView.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textviewValueChange:) name:UITextFieldTextDidChangeNotification object:self.addressText];
    self.addressTextView.userInteractionEnabled = NO;
    self.addressTextView.textColor = [UIColor grayColor];
    self.addressText.textColor = [UIColor grayColor];
    self.proviceLabel.textColor = [UIColor grayColor];
    isClicked_ = NO;
    [self.view addSubview:self.pickerView];
    self.dataRequest.delegate = self;
}

- (AddressPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[AddressPickerView alloc]initWithFrame:CGRectMake(0, Screen_height , Screen_width, 215)];
        _pickerView.delegate = self;
    }
    return _pickerView;
}

#pragma mark -- click
- (void)saveButtonClick:(UIButton *)sender {

    if (self.usernameText.text.length == 0) {
        
        [XHToast showBottomWithText:@"请输入收货人名字"];
        return;
    }
    if (self.phoneText.text.length == 0) {
        
        [XHToast showBottomWithText:@"请输入联系方式"];
        return;
    }
    if (![PublicFunction checkTelNumber:self.phoneText.text]) {
        
        [XHToast showBottomWithText:@"手机号码格式不正确"];
        return;
    }
    if (self.addressString.length == 0) {
        
        [XHToast showBottomWithText:@"请选择地址"];
        return;
    }
    if (self.addressText.text.length == 0) {
        
        [XHToast showBottomWithText:@"请输入详细地址"];
        return;
    }
    [self.dataRequest post_HCMineUploadAddress:HCGETUSER(@"userid") address:self.finalyAddrss phone:self.phoneText.text name:self.usernameText.text];
}

- (void)viewClickAction:(UITapGestureRecognizer *)gesture {

    if (!isClicked_) {
        
        //显示
        [self.pickerView show];
    }
    else {
    
        //消失
        [self.pickerView hide];
    }
    isClicked_ = !isClicked_;
    [self.addressText resignFirstResponder];
    [self.phoneText resignFirstResponder];
    [self.usernameText resignFirstResponder];
}

#pragma mark - AddressPickerViewDelegate
- (void)cancelBtnClick{
    NSLog(@"点击了取消按钮");
    [self.pickerView hide];
    isClicked_ = NO;
}
- (void)sureBtnClickReturnProvince:(NSString *)province City:(NSString *)city Area:(NSString *)area{

    self.addressString = [NSString stringWithFormat:@"%@%@%@",province,city,area];
    [self.pickerView hide];
    isClicked_ = NO;
    self.proviceLabel.text = self.addressString;
    self.addressTextView.text = [NSString stringWithFormat:@"%@%@",self.addressString,self.addressText.text];
    self.finalyAddrss = [NSString stringWithFormat:@"%@%@",self.addressString,self.addressText.text];
}

- (void)textviewValueChange:(NSNotification *)obj {

    NSLog(@"address:%@",self.addressText.text);
    self.addressTextView.text = [NSString stringWithFormat:@"%@%@",self.addressString,self.addressText.text];
    self.finalyAddrss = [NSString stringWithFormat:@"%@%@",self.addressString,self.addressText.text];
}

- (void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {

    if (aType == HC_MINEUPLOADADDRESS_TYPE) {
        
        if ([dictionary[@"stat"] integerValue] == 200) {
            
            [XHToast showBottomWithText:@"保存成功"];
            dispatch_time_t time=dispatch_time(DISPATCH_TIME_NOW, 0.5*NSEC_PER_SEC);
            dispatch_after(time, dispatch_get_main_queue(), ^{
            
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    }
}

- (void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {

    [XHToast showBottomWithText:@"上传失败"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
